import { defineStore } from 'pinia'
import { login } from "@/api/module/BaseLogin";
import { message } from "ant-design-vue";
import {
  getStorage,
  setStorage,
  StorageName,
  delStorage
} from "@/tools/storage.js";


// function SET_USER_TOKEN(state, token) {
//   if (token) {
//     state.token = token;
//     setStorage(StorageName.user_token, token);
//   } else {
//     state.token = "";
//     delStorage(StorageName.user_token);
//   }
// }
// function SET_USER_INFO(state, userInfo) {
//   if (userInfo) {
//     state.userInfo = userInfo;
//     setStorage(StorageName.user_info, userInfo);
//   } else {
//     state.userInfo = "";
//     delStorage(StorageName.user_info);
//   }
// }
export const userCounterStore = defineStore('user', {
  state: () => ({
    // 用户的token
    token: localStorage.getItem("USER_TOKEN") != null ? localStorage.getItem("USER_TOKEN") : "",
    // 用户的详细信息
    userInfo: localStorage.getItem('USER_INFO') != null ? JSON.parse(localStorage.getItem('USER_INFO')) : null
  }),
  getters: {

  },
  actions: {
    async setUserToken(token) {
      this.token = token;
    },
    async logout() {
      message.success("注销成功", 0.5).then(function () {
        delStorage(StorageName.user_token);
        delStorage(StorageName.user_info);
        window.location.reload();
      });
    },

    async login(data) {
      try {
        const response = await login(data)
        const { code, msg, data: userInfo } = response
        // console.log("查看user.js中的login方法的返回值：", response);
        if (code === 0) {
          const { token, permissions } = userInfo
          delete userInfo.permissions
          delete userInfo.token
          // 将token存储到本地
          // if (token) {
          this.token = token
          setStorage(StorageName.user_token, token)
          // } else {
          //   this.token = ""
          //   delStorage(StorageName.user_token)
          // }
          // if (userInfo) {
          this.userInfo = userInfo
          setStorage(StorageName.user_info, userInfo)
          // } else {
          //   this.userInfo = ""
          //   delStorage(StorageName.user_info)
          // }

          return userInfo
        } else {
          return Promise.reject(msg)
        }
      } catch (e) {
        console.log(e)
      }
    }
  }
})


// const state = {
//   onlineUser: [],
//   token: localStorage.getItem("USER_TOKEN") != null ? localStorage.getItem("USER_TOKEN") : "",
//   userInfo: localStorage.getItem('USER_INFO') != null ? JSON.parse(localStorage.getItem('USER_INFO')) : null,
//   userRoutes: localStorage.getItem("USER_ROUTES") != null ? JSON.parse(localStorage.getItem("USER_ROUTES")) : [],
//   tabs: localStorage.getItem("USER_TABS") != null ? JSON.parse(localStorage.getItem("USER_TABS")) : { list: [], active: { path: "", query: "" } },  //标签页
// }

// const mutations = {
//   SET_ONLINE_USER(state, users) {
//     state.onlineUser = users
//   },
//   SET_USER_TOKEN(state, token) {
//     if (token) {
//       state.token = token;
//       localStorage.setItem('USER_TOKEN', token);
//     } else {
//       state.token = '';
//       localStorage.removeItem('USER_TOKEN')
//     }
//   },
//   SET_USER_INFO(state, userInfo) {
//     if (userInfo) {
//       state.userInfo = userInfo
//       localStorage.setItem('USER_INFO', JSON.stringify(userInfo))
//     } else {
//       state.userInfo = '';
//       localStorage.removeItem('USER_INFO')
//     }

//   },
//   SET_USER_MENU(state, menuList) {
//     if (menuList) {
//       //删除时间字段
//       menuList.forEach(item => {
//         delete item.create_time;
//         delete item.update_time;
//       })
//       state.userRoutes = menuList
//       localStorage.setItem('USER_ROUTES', JSON.stringify(menuList))
//     } else {
//       state.userRoutes = []
//       localStorage.removeItem('USER_ROUTES')
//     }
//   },
//   SET_USER_TABS(state, tabs) {
//     if (tabs) {
//       state.tabs = tabs
//       localStorage.setItem('USER_TABS', JSON.stringify(tabs))
//     } else {
//       state.tabs = []
//       localStorage.removeItem('USER_TABS')
//     }
//   }

// }

// const actions = {
//   setUserToken({ commit }, token) {
//     return new Promise(resolve => {
//       commit('SET_USER_TOKEN', token);
//       resolve()
//     })
//   },
//   async logout({ commit }) {
//     message.success("注销成功", 0.5).then(function () {
//       commit('SET_USER_TOKEN');
//       commit('SET_USER_INFO');
//       commit('SET_USER_MENU');
//       window.location.reload();
//     });
//     return Promise.resolve();
//   },
//   async login({ commit }, data) {
//     try {
//       const response = await login(data)
//       const { code, msg, data: userInfo } = response
//       if (code === 0) {
//         const { token, permissions } = userInfo
//         delete userInfo.permissions
//         delete userInfo.token
//         commit('SET_USER_TOKEN', token)
//         commit('SET_USER_INFO', userInfo)
//         commit('SET_USER_MENU', permissions)  //缓存list路由
//         return Promise.resolve(userInfo)
//       } else {
//         return Promise.reject(msg)
//       }
//     } catch (e) {
//       console.log(e)
//     }
//   },
//   async reloadInfo({ commit }) {
//     try {
//       const response = await reloadinfo()
//       const { code, msg, data: userInfo } = response
//       if (code === 0) {
//         const { permissions } = userInfo
//         delete userInfo.permissions
//         delete userInfo.token
//         commit('SET_USER_INFO', userInfo)
//         commit('SET_USER_MENU', permissions)  //缓存list路由

//         console.log("MENG===", permissions);
//         message.success("clear success", 0.5).then(function () {
//           window.location.reload();
//         });
//         return Promise.resolve()
//       } else {
//         return Promise.reject(msg)
//       }
//     } catch (e) {
//       console.log(e)
//     }
//   },
// }

// export default {
//   namespaced: true,
//   mutations,
//   actions,
//   state
// }